package com.soujiayi.activity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.soujiayi.coupon.CouponSimpleAdapter;
import com.soujiayi.coupon.Utils;

public class CouponActivity extends Activity {
	private ImageView imgHome;
	private Button butBack;
	private TextView textCouponLabel;
	private ListView couponList;
	private String strLabelSum;
	private CouponSimpleAdapter adapter;
	private ArrayList<Map<String, Object>> list;
	private Activity mActivity=CouponActivity.this;
	String resStrCouponData = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.coupon);
	
		initViewByOrder();
//		getResponse();//得到返回数据
//		getSaxGetData(resStrCouponData);//解析数据
		
	     getListModel();//测试
		initCouponAdapter();
        initActionListener();
	}

	public void initViewByOrder() {
	
		list = new ArrayList<Map<String, Object>>();
//      list=getListModel();
	
//      	boolean b=	NetworkUtils.isNetworkConnected(mActivity);
//      	System.out.println(b);
		imgHome = (ImageView) findViewById(R.id.img_coupon_home);
		butBack = (Button) findViewById(R.id.bt_coupon_back);
		textCouponLabel = (TextView) findViewById(R.id.tv_coupon_label);
		couponList = (ListView) findViewById(R.id.coupon_list);
	}



	public void initActionListener() {
		imgHome.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent backHome = new Intent(CouponActivity.this,
						IndexActivity.class);
				startActivity(backHome);

			}
		});

		butBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				 CouponActivity.this.finish();
				}
		});

		couponList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			
				textCouponLabel.setText(showListSize(true,list.size(),arg2+1));
				Intent couponDetail = new Intent(CouponActivity.this,
						CouponDetailActivity.class);
			//	couponDetail.setData(data);
				startActivity(couponDetail);

			}
		});

	}

	public void initCouponAdapter() {
		
	
		adapter = new CouponSimpleAdapter(CouponActivity.this, list,
				R.layout.coupon_item, new String[] { "img", "title", "free",
						"from", "curcent" }, new int[] {
						R.id.img_coupon_item_photo, R.id.tv_coupon_item_title,
						R.id.tv_coupon_item_free, R.id.tv_coupon_item_from,
						R.id.tv_coupon_item_sell_status });
		couponList.setAdapter(adapter);
	
		textCouponLabel.setText(showListSize(false,list.size(),0));
	}
	public  ArrayList<Map<String, Object>> getListModel() {
		 ArrayList<Map<String, Object>>  arrayList=new ArrayList<Map<String, Object>>();
		 Map<String, Object> map=new HashMap<String, Object>();
	   Bitmap  bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.coupon_demo1);
		 map.put("img",bitmap);
		 map.put("title","中国鲜花网   满100减10元");
		 map.put("free","免费");
		 map.put("from","来自：中国鲜花网");
		 map.put("curcent","已售出8笔");
		 arrayList.add(map);
		 map=new HashMap<String, Object>();
		 map.put("img",R.drawable.coupon_demo2);
		 map.put("title","博洋家纺   满100减15元");
		 map.put("free","免费");
		 map.put("from","来自：中国信息网");
		 map.put("curcent","已售出8笔");
		 arrayList.add(map);
		
		 
		 return arrayList;
	}
	public String getResponse() {

		String path ="http://coupon.soujiayi.com/";
		try {
			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpParams params = httpclient.getParams();
			HttpGet request = new HttpGet(path);
			request.setHeader("charset", "utf-8");

			HttpConnectionParams.setConnectionTimeout(params, 5000);
			HttpResponse response = httpclient.execute(request);
		/*	
			InputStream is = new ByteArrayInputStream(
					response.toString().getBytes("UTF-8"));*/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				resStrCouponData = EntityUtils.toString(response.getEntity());
			
			} else {
				Toast.makeText(this, "连接异常，请检查网络", Toast.LENGTH_SHORT).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resStrCouponData;

	}
	
	private void getSaxGetData(String str)
	{
		if(str == null){
			Utils.showIntentAlert(mActivity);
		}

		
		
	}
	
	
	
	
	
	private String showListSize(Boolean b,int sumCouponCount,int currentCouponCount){
		
		strLabelSum = "共搜索到" + sumCouponCount + "个优惠券";
		if(b==true){
			strLabelSum+="--当前点击" + currentCouponCount
					+ "个";
		}
	 return strLabelSum;
		
	}
	//程序结束对话框</LinearLayout>
	private void overDialog(){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle(getResources().getString(R.string.prompt)).setMessage(getResources().getString(R.string.prompt_close))
		.setPositiveButton(getResources().getString(R.string.qd), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//thread.destroy();
				
				SoujiayiLoginActivity.couponID=null;
				CouponActivity.this.finish();
			}
		}).setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		AlertDialog alertDialog = builder.create();
		builder.show();
	}
	
	}
